using AiQiuQuan.Sport.Model.Models;
using AiQiuQuan.Sport.Core;
using AiQiuQuan.Sport.WebApi.Services;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Authorization;

namespace AiQiuQuan.Sport.WebApi.Controllers
{
    /// <summary>
    /// 课程管理
    /// </summary>
    public class CourseController : ApiController
    {
        private readonly ICourseService _courseService;
        private readonly ICourseOrderService _courseOrderService;

        /// <summary>
        /// 课程管理
        /// </summary>
        public CourseController(
            ICourseService courseService,
            ICourseOrderService courseOrderService)
        {
            _courseService = courseService;
            _courseOrderService = courseOrderService;
        }

        /// <summary>
        /// 查询课程及附加信息
        /// </summary>
        [HttpPost("query_full")]
        [AllowAnonymous]
        public async Task<UnaryResult<CourseDto>> QueryCouseAndAttachInfoAsync(CourseInfoRequestDto request)
        {
            var result = await _courseService.QueryCouseAndAttachInfoAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 查询课程信息
        /// </summary>
        [HttpPost("query")]
        public async Task<UnaryResult<CourseDto>> QueryCouseAsync(CourseInfoRequestDto request)
        {
            var result = await _courseService.QueryCouseAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 获取课程购买信息
        /// </summary>
        /// <param name="courseDetailId">课程明细ID</param>
        [HttpGet("get_buy_info")]
        public async Task<UnaryResult<CouseBuyInfoDto>> GetCourseBuyInfo([Required] Guid courseDetailId)
        {
            var result = await _courseService.GetCourseBuyInfoAsync(courseDetailId);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 课程购买
        /// </summary>
        [HttpPost("buy")]
        public async Task<UnaryResult<CourseBuyResultDto>> BuyAsync(CourseBuyRequestDto request)
        {
            var result = await _courseService.BuyAsync(request);
            return result;
        }

        /// <summary>
        /// 获取课程教练列表
        /// </summary>
        /// <param name="courseId">课程ID</param>
        [HttpGet("get_coacher_list")]
        public async Task<UnaryResult<List<CourseCoacherDto>>> QueryCourseCoacherListAsync([Required] Guid courseId)
        {
            var result = await _courseService.QueryCourseCoacherListAsync(courseId);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 获取课程学生列表
        /// </summary>
        /// <param name="courseId">课程ID</param>
        [HttpGet("get_student_list")]
        public async Task<UnaryResult<List<CourseStudentDto>>> QueryCourseStudentListAsync([Required] Guid courseId)
        {
            var result = await _courseService.QueryCourseStudentListAsync(courseId);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 课程学生记录
        /// </summary>
        [HttpPost("query_student_record_list")]
        public async Task<UnaryResult<PageResult<CourseStuentRecordDto>>> QueryCourseStuentRecordListAsync(CourseStudentRecordPageRequestDto request)
        {
            var result = await _courseService.QueryCourseStuentRecordListAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 课程分页列表
        /// </summary>
        [HttpPost("pagelist")]
        [AllowAnonymous]
        public async Task<UnaryResult<PageResult<CourseDto>>> PageListAsync(CoursePageRequestDto request)
        {
            var result = await _courseService.PageListAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 获取我的课程计划列表(团体课程、私教)
        /// </summary>
        [HttpGet("list_plan_self")]
        public async Task<UnaryResult<List<MyCoursePlanDto>>> GetMyCoursePlanListAsync()
        {
            var result = await _courseService.GetMyCoursePlanListAsync();
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 教练课程列表
        /// </summary>
        [HttpPost("pagelist_coacher")]
        public async Task<UnaryResult<PageResult<CourseDto>>> GetCoacherCourseListAsync(CourseSelfPageRequestDto request)
        {
            var result = await _courseService.GetCoacherCourseListAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 课程订单分页查询
        /// </summary>
        [HttpPost("pagelist_order")]
        public async Task<UnaryResult<PageResult<CourseOrderDto>>> PageListAsync(CourseOrderPageRequestDto request)
        {
            request.IsSelf = true;
            var result = await _courseOrderService.PageListAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 课程订单查询
        /// </summary>
        [HttpGet("get_order")]
        public async Task<UnaryResult<CourseOrderDto>> GetCourseOrderAsync(Guid orderId)
        {
            var result = await _courseOrderService.GetAsync(orderId);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 课程相关状态、标记修改
        /// </summary>
        [HttpPost("update_flag")]
        public async Task<UnaryResult> UpdateCourseFlagAsync(CourseFlagUpdateDto request)
        {
            var result = await _courseService.UpdateCourseFlagAsync(request);
            return result;
        }

        /// <summary>
        /// 教练课程计划状态修改
        /// </summary>
        [HttpPost("update_plan_state")]
        public async Task<UnaryResult> UpdateCoursePlanStateAsync(CoursePlanStateUpdateDto request)
        {
            var result = await _courseService.UpdateCoursePlanStateAsync(request);
            return result;
        }

        /// <summary>
        /// 学生点名
        /// </summary>
        [HttpPost("call_student")]
        public async Task<UnaryResult> CallStudentAsync(CourseStudentRequestDto request)
        {
            var result = await _courseService.CallStudentAsync(request);
            return result;
        }

        /// <summary>
        /// 课程学生删除
        /// </summary>
        [HttpPost("delete_student")]
        public async Task<UnaryResult> DeleteStudentAsync(CourseStudentDelDto request)
        {
            var result = await _courseService.DeleteStudentAsync(request);
            return result;
        }
    }
}
